
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{
	[SqlProcedure]
	public static void SpMediaNemN(int n, out SqlSingle med)
	{
		// Put your code here
		int usa = 0;
		int cont = 0;
		float f = 0;
        SqlConnection cn = new SqlConnection("context connection=true");
        cn.Open();
        SqlCommand cmd = cn.CreateCommand();
        cmd.CommandText = "Select valor from tabela1";
		SqlDataReader rdr = cmd.ExecuteReader();
		
		while (rdr.Read())
		{ 
			if (usa == 0)
			{
				f += rdr.GetFloat(0);
				++cont;
			}
			usa = (usa + 1) % n;
			
		}
		rdr.Close();
		med = (cont == 0)? -1: f / cont;
	}
}
